import Game from "../../Script/Game/Game";
import NodeUtil from "../../Script/framework/utils/NodeUtil"; 
import { PetData } from "../../Script/Game/Entity/PetData";
import { GData } from "../../Script/framework/utils/GData";
import EventManager from "../../Script/framework/core/EventManager";
import { GameEvent } from "../../Script/constants/Events";
import Fighter from "./Fighter";
import { SPINE_ACTION } from "../../Script/framework/utils/Define";

 

/**
 * 战机位置
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class Slot extends cc.Component {
    

    @property(cc.Label)
    levelLab:cc.Label = null;
    
    @property(cc.Node)
    point:cc.Node = null;
 
    @property(cc.Prefab)
    fighterPre:cc.Prefab= null;


    fighter:cc.Node = null; 
    fightersPool = []; 
    _touchId:any = null; 
    _touchMoved:boolean = false;
    idx:number = 0;

    //游戏控制器
    game:Game = null;
    //触摸点位置
    oldPos:cc.Vec2 = cc.v2();
    //阵上数据
    data:PetData = null;

    onLoad () {  
        this.reg();
    }

    reg(){ 
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this);
    }

    unreg(){
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegan, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnded, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchEnded, this);
    }
     
    onDestroy () {  
       this.unreg();
    }

    _onTouchBegan( e ){
        if(!this.isExistFighter()) return;
        this.node.zIndex = 1000;
        this.oldPos.x = e.currentTouch._point.x
        this.oldPos.y = e.currentTouch._point.y
        this._touchMoved = true;
    }

    _onTouchMoved( e ){  
        if(!this.isExistFighter()) return; 
        if( this._touchMoved && this.fighter){
            this.fighter.getComponent(Fighter).playAni(SPINE_ACTION.WAIT_FLY);
        }
        this.fighter.x = e.currentTouch._point.x - this.oldPos.x
        this.fighter.y = e.currentTouch._point.y - this.oldPos.y
    }

    _onTouchEnded( e ){
        this._touchMoved = false; 
        this.node.zIndex = 0; 
        if(!this.isExistFighter()) return;
        this.fighter.x = e.currentTouch._point.x - this.oldPos.x
        this.fighter.y = e.currentTouch._point.y - this.oldPos.y 
        let node = this.fighter.convertToWorldSpaceAR(cc.v2(0,0))
        let isContain = NodeUtil.isPosOnNodeRect(cc.v2(node.x,node.y), this.game.battleScene);
        if(isContain){  
            //删除 加入战场 
            let info:PetData = GData.Slots[this.idx];
            info.inField = true;
            info.idx = -1;
            //转化下坐标
            let pos =  NodeUtil.getRelativePosition(this.fighter,this.game.battleScene) 
            EventManager.emit(GameEvent.ADD_FIGHT,pos,info);
            this.clearFighter();
        }else{
            //检查是否交换位置 
            this.game.exchangeFighterMovelLogic(this);
        }
    }

    clearFighter(){
        this.data = null;
        GPool.put(this.fighter);
        this.fighter = null;
        this.levelLab.string = "";
    }
  
    updateSlot(){   
        if(this.data){
            if(!this.fighter){
                this.initSpine();
                return;
            } 
            this.levelLab.string = `${this.data.level}`;   
            this.fighter.position = cc.v3(0,0)  
            this.fighter.getComponent(Fighter).playAni(SPINE_ACTION.WAIT);
        }  
    }
    
    setData(option){  
        this.game = option.obj;
        this.idx = option.idx;  
        this.data = option.data;
        this.initSpine();
    }

    updateData(data){
        this.data = data;
        this.updateSlot();
    }


    initSpine(){
        //创建一个自己 
        GPool.get("Fighter",{setData:{game:this.game}}).then((node:cc.Node)=>{
            this.fighter = node;
            node.parent = this.point;
            this.updateSlot();
        })
    }


    isExistFighter(){
        return this.fighter && this.fighter.active
    }
    //重置战机位置
    resetFighterPos(){
        if (this.isExistFighter()) {
            this.fighter.position = cc.v3(0,0);
        }
    }


    playAni(name:SPINE_ACTION){
        if(this.fighter){
            this.fighter.getComponent(Fighter).playAni(name);
        } 
    }


 
}
